package com.admin.exam.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.admin.exam.entity.HospitalRank;
import com.admin.exam.service.HospitalService;
import com.admin.system.entity.SystemLog;
import com.admin.system.vo.query.SystemLogQuery;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.admin.base.Page;
import com.admin.base.BaseController;
import com.admin.base.Response;
import com.admin.exam.entity.PersonInfo;
import com.admin.exam.service.PersonInfoService;
import com.admin.exam.vo.query.PersonInfoQuery;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 *
 */
@Controller
@RequestMapping(value = "/exam/personInfo")
public class PersonInfoController extends BaseController {

    @Autowired
    PersonInfoService personInfoService;
    @Autowired
    HospitalService hospitalService;

    /**
     * 列表页面
     */
    @RequestMapping(value = "/list")
    public ModelAndView list(HttpServletRequest request, HttpServletResponse response, ModelAndView mav) {
        mav.setViewName("exam/personInfo/list");
        return mav;
    }

    /**
     * 分页查询数据
     */
    @RequestMapping(value = "/paging")
    @ResponseBody
    public Response paging(HttpServletRequest request, HttpServletResponse response, PersonInfoQuery query) {
        Page<PersonInfo> page = personInfoService.pagingByConditions(query);
        return Response.ok(page);
    }

    /**
     * 新增页面
     */
    @RequestMapping(value = "/toAdd")
    public ModelAndView toAdd(HttpServletRequest request, HttpServletResponse response, ModelAndView mav) {
        mav.setViewName("exam/personInfo/add");
        return mav;
    }

    /**
     * 保存数据
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Response add(HttpServletRequest request, HttpServletResponse response, PersonInfo personInfo) {
        personInfoService.add(personInfo);
        return Response.ok();
    }

    /**
     * 删除数据
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Response delete(HttpServletRequest request, HttpServletResponse response, Integer[] id) {
        personInfoService.delete(id);
        return Response.ok();
    }

    /**
     * 编辑页面
     */
    @RequestMapping(value = "/toEdit")
    public ModelAndView toEdit(HttpServletRequest request, HttpServletResponse response, ModelAndView mav, Integer id) {
        PersonInfo personInfo = personInfoService.getById(id);
        mav.addObject(personInfo);
        mav.setViewName("exam/personInfo/edit");
        return mav;
    }

    /**
     * 详情页面
     */
    @RequestMapping(value = "/detail")
    public ModelAndView detail(HttpServletRequest request, HttpServletResponse response, ModelAndView mav, Integer id) {
        PersonInfo personInfo = personInfoService.getById(id);
        mav.addObject(personInfo);
        mav.setViewName("exam/personInfo/detail");
        return mav;
    }

    /**
     * 更新数据
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Response update(HttpServletRequest request, HttpServletResponse response, PersonInfo personInfo) {
        personInfoService.update(personInfo);
        return Response.ok();
    }


    @GetMapping(value = "/download")
    public void download(HttpServletRequest request, HttpServletResponse response, SystemLogQuery query) throws IOException {
        ExcelWriter excelWriter = null;

        List<PersonInfo> personalRank = personInfoService.getPersonalRank();

        List<HospitalRank> hospitalRank = hospitalService.getHospitalRank(personalRank);

        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("rank", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        excelWriter = EasyExcel.write(response.getOutputStream()).build();
        WriteSheet writeSheet1 = EasyExcel.writerSheet(0, "个人排行").head(PersonInfo.class).build();
        excelWriter.write(personalRank, writeSheet1);

        WriteSheet writeSheet2 = EasyExcel.writerSheet(1, "医院排行").head(HospitalRank.class).build();
        excelWriter.write(hospitalRank, writeSheet2);
        excelWriter.finish();
    }


}
